package linkwechat.service.custormer;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import linkwechat.mapper.Customer.SeasMapper;
import linkwechat.pojo.CustomersSeas;
import linkwechat.pojo.ReturnData;
import linkwechat.pojo.ReturnEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientService {

    @Autowired
    SeasMapper seasMapper;

    public ReturnData selectClinet(CustomersSeas customersSeas) {
        PageHelper.startPage(customersSeas.getPage(),customersSeas.getSize());
        List<CustomersSeas> customersSeas1 = seasMapper.SelectClinet(customersSeas);
        PageInfo<CustomersSeas> customersSeasPageInfo = PageInfo.of(customersSeas1);
        return ReturnData
                .builder()
                .code(ReturnEnum.QUERY_SUCCEED.getCode())
                .msg(ReturnEnum.APPEND_SUCCEED.getMsg())
                .data(customersSeasPageInfo)
                .build();
    }


    public ReturnData ImportClient(List<CustomersSeas> userList) {
        Integer i = 0;
        if (userList.size()>0){
            for (CustomersSeas seas : userList) {
                 i += seasMapper.ImportCustomers(seas);
            }
        }

        if (i == userList.size()){
             return ReturnData
                     .builder()
                     .code(ReturnEnum.APPEND_SUCCEED.getCode())
                     .msg(ReturnEnum.APPEND_SUCCEED.getMsg())
                     .data(i)
                     .build();
        }
        return ReturnData
                .builder()
                .data(0)
                .build();
    }

    public ReturnData DeleteManyClients(Long[] ids) {
        Integer i = 0;
        for (Long id : ids) {
           i += seasMapper.deleteClients(id);
        }
        if (i==ids.length){
            return ReturnData
                    .builder()
                    .code(ReturnEnum.REMOVE_SUCCEED.getCode())
                    .msg(ReturnEnum.REMOVE_SUCCEED.getMsg())
                    .data(i)
                    .build();
        }
        return ReturnData
                .builder()
                .data(0)
                .build();
    }
}
